package com.scaffold.myproject.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 */
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1800935089461387955L;

    /**
     * 总记录数
     */
    private Integer total;
    /**
     * 每页记录数
     */
    private Integer pageSize;
    /**
     * 当前页数
     */
    private Integer pageNo;
    /**
     * 列表数据
     */
    private List<T> records;

    /**
     * 分页
     *
     * @param records    当前页列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param currPage   当前页数
     */
    public PageResult(List<T> records, int totalCount, int pageSize, int currPage) {
        this.records = records;
        this.total = totalCount;
        this.pageSize = pageSize;
        this.pageNo = currPage;
    }

    /**
     * 分页（主要用于内存分页, 每次都是查询全部数据再分页）
     *
     * @param records  总的列表数据
     * @param pageSize 每页记录数
     * @param currPage 当前页数
     */
    public PageResult(List<T> records, int pageSize, int currPage) {
        if (records == null) {
            records = new ArrayList();
        }
        this.total = records.size();
        this.pageSize = pageSize;
        this.pageNo = currPage;
        //总记录数和每页显示的记录之间是否可以凑成整数(pages)
        boolean full = this.total % pageSize == 0;
        int pages = 0;
        if (!full) {
            pages = this.total / pageSize + 1;
        } else {
            pages = this.total / pageSize;
        }
        int fromIndex = 0;
        int toIndex = 0;
        fromIndex = pageNo * pageSize - pageSize;
        if (this.pageNo <= 0) {
            //如果查询的页数小于等于0，则展示第一页
            this.pageNo = 1;
            toIndex = pageSize;
        } else if (this.pageNo >= pages) {
            this.pageNo = pages;
            toIndex = this.total;
        } else {
            toIndex = this.pageNo * this.pageSize;
        }
        if (records.size() == 0) {
            this.records = records;
        } else {
            this.records = records.subList(fromIndex, toIndex);
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "total=" + total +
                ", pageSize=" + pageSize +
                ", pageNo=" + pageNo +
                ", records=" + records +
                '}';
    }
}